#include "../thread_pool.h"
#include "mysql.h"
#include "../user.h"
extern char server_wd[1024];

//进入某一个目录后，删除一个文件，
//如果该文件是普通文件则直接删除其记录
//如果该文件是一个目录，则需要递归删除
//对虚拟文件表的操作
/*----------------------------delimiter---------------*/
int select_file_cd(user_info * user,char * dirname){
    //从filetable中通过user的id和pwd确定文件名
     connect_mysql(user);
    //初始化sql
    const char* sql = "SELECT type  FROM virtual_file_table WHERE owner_id = ? and pwd = ? and filename = ?\0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 3) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    int user_id = 0;
    unsigned long user_id_len = 0;
    char user_pwd[100] = {0};
    unsigned long pwd_len = 0;
    char dir[100] = {0};
    unsigned long dir_len = 0;
    //绑定参数
    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &user_id_len;//整型数据不需要指定长度缓冲区

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = user_pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[2].buffer = dir;
    bind[2].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[2].length = &dir_len;//整型数据不需要指定长度缓冲区
    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    user_id_len = 4;
    strcpy(user_pwd,user->pwd);
    pwd_len = strlen(user_pwd);
    strcpy(dir,dirname);
    dir_len = strlen(dir);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_type[100] = {0};
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[0].buffer = res_type;
    res_bind[0].buffer_length = sizeof(res_type);//确定值
    //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    int ans = 0;
    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        //判断读取的res是否为空
        if(!strcmp(res_type,"d")){
            ans = 1;
        }
       }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
    return ans;
}
/*--------------------delimiter-----------------------*/
void select_file_ls(user_info * user){
    //从filetable中通过user的id和pwd确定文件名
     connect_mysql(user);
    //初始化user
    const char* sql = "SELECT filename FROM virtual_file_table WHERE owner_id = ? and pwd = ?\0";
    printf("user_id is %d\n",user->user_id);
    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    int user_id = 0;
    unsigned long user_id_len = 0;
    char user_pwd[100] = {0};
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &user_id_len;//整型数据不需要指定长度缓冲区

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = user_pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    user_id_len = 4;
    strcpy(user_pwd,user->pwd);
    pwd_len = strlen(user_pwd);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_filename[100] = {0};
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[0].buffer = res_filename;
    res_bind[0].buffer_length = sizeof(res_filename);//确定值
    //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        //经由user的peerfd循环发送至client
        train_t t;
        memset(&t,0,sizeof(t));
        t.len = strlen(res_filename);
        t.type =CMD_TYPE_LS;
        strcpy(t.buff,res_filename);
        sendn(user->peerfd,&t,8+t.len);
    }
    //最后发送一次表示结束
    train_t t;
    memset(&t,0,sizeof(t));
    t.len = 0;
    t.type =CMD_TYPE_LS;
    sendn(user->peerfd,&t,8);

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
}
/*---------------------delimiter-----------------*/
int select_parent_id(user_info* user) {
    //从filetable中通过user的id和pwd确定文件名
     connect_mysql(user);
    //初始化user
    const char* sql = "SELECT DISTINCT parent_id FROM virtual_file_table WHERE owner_id = ? and pwd = ?\0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    int user_id = 0;
    unsigned long user_id_len = 0;
    char user_pwd[100] = {0};
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &user_id_len;//整型数据不需要指定长度缓冲区

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = user_pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    user_id_len = 4;
    strcpy(user_pwd,user->pwd);
    pwd_len = strlen(user_pwd);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    int res_parentid = 0;
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_LONG;
    res_bind[0].buffer = &res_parentid;
    res_bind[0].buffer_length = sizeof(res_parentid);//确定值
    //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //读取输出的数据
    int ans = -1;
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        ans = res_parentid;
    }
    //最后发送一次表示结束
    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);

    if(ans != -1){
        printf("ans is %d\n",ans);
        return ans;
    }else{
    //此时目录为空，要拆分前一级目录获得id作为parent_id
        //如果当前目录是根目录直接返回parentid为0
        if(!strcmp(user->pwd,server_wd)){
            return 0;
        }else{
            //获取上级目录
            char parent_wd[1024] = {0};
            strcpy(parent_wd,user->pwd);
            printf("orignal wd is %s\n",parent_wd);
            back_pre_wd(parent_wd);
            printf("parent_wd is %s\n",parent_wd);
            int ret = select_file_id(user,parent_wd);
            printf("ret is %d\n",ret);
            return ret;
        }
    }
}
/*---------------------delimiter-----------------*/
int select_file_id(user_info* user,char *parent_wd){
    connect_mysql(user);
    //初始化user
    const char* sql = "SELECT DISTINCT id FROM virtual_file_table WHERE owner_id = ? and pwd = ? ";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int user_id = 0;
    char pwd[200] = { 0 };
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = NULL;//整型数据不需要指定长度缓冲区
                          //
    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    strcpy(pwd, parent_wd);
    pwd_len = strlen(pwd);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    int res_id = 0;
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数
    res_bind[0].buffer_type = MYSQL_TYPE_LONG;
    res_bind[0].buffer = &res_id;
    res_bind[0].buffer_length = sizeof(res_id);//确定值,空间要大
                                                 //
                                                 //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //读取输出的数据
    int ans = -1;
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        ans = res_id;
    }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
    return ans;
}


/*---------------------delimiter-----------------*/
//由于是从服务端下载文件，若该文件存在则一定存在虚拟表中
//如是，则虚拟表中不存在，则该文件一定不存在
//根据文件名，owner_id，pwd来获取文件的MD5
void select_file_md5from(user_info* user, const char* filename, char* md5) {
    connect_mysql(user);
    //初始化user
    const char* sql = "SELECT md5 FROM virtual_file_table WHERE owner_id = ? and pwd = ? and filename = ?";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 3) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int id = 0;
    char pwd[200] = { 0 };
    unsigned long pwd_len = 0;
    char filenames[100] = { 0 };
    unsigned long filename_len = 0;

    //绑定参数
    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = NULL;//整型数据不需要指定长度缓冲区
                          //
    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区
                              //
    bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[2].buffer = filenames;
    bind[2].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[2].length = &filename_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    id = user->user_id;
    strcpy(pwd, user->pwd);
    pwd_len = strlen(pwd);
    strcpy(filenames, filename);
    filename_len = strlen(filenames);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_buff[100] = { 0 };
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数
    res_bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[0].buffer = &res_buff;
    res_bind[0].buffer_length = sizeof(res_buff);//确定值,空间要大
                                                 //
                                                 //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }

        strcpy(md5, res_buff);
    }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
}

void select_issame_from_vartual_table(user_info* user,int* id, int parentid, const char* filename) {
    connect_mysql(user);
    //初始化user
    const char* sql = "SELECT id FROM virtual_file_table WHERE parent_id = ? and filename = ?";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int index = 0;
    char filenames[100] = { 0 };
    unsigned long filename_len = 0;

    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &index;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = NULL;//整型数据不需要指定长度缓冲区
                          //
    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = filenames;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &filename_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    index = parentid;
    strcpy(filenames, filename);
    filename_len = strlen(filenames);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_id = 0;
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数
    res_bind[0].buffer_type = MYSQL_TYPE_LONG;
    res_bind[0].buffer = &res_id;
    res_bind[0].buffer_length = sizeof(res_id);//确定值,空间要大
                                                 //
                                                 //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }

        *id = res_id;
    }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);

}
